490B - Queue - CodeForces Solution


dsu implementation *1500

Please click on ads to support us..

Python Code:

import sys

def organize_queue(left_dict, right_dict, first, last, n):
    queue = [0] * n
    queue[0] = first
    left = first
    right = 0
    idx = 2
    
    while idx < n:
        right = right_dict[left]
        queue[idx] = right
        left = right
        idx += 2
    
    right = right_dict[0]
    queue[1] = right
    left = right
    idx = 3
    while idx < n:
        right = right_dict[left]
        queue[idx] = right
        left = right
        idx += 2

    return queue

def main():
    input = sys.stdin.read
    data = input().strip().split('\n')

    n = int(data[0])
    students = [tuple(map(int, line.split())) for line in data[1:]]

    left_dict = {}
    right_dict = {}

    for a, b in students:
        left_dict[b] = a
        right_dict[a] = b

    left_set = set(left_dict.values())
    right_set = set(right_dict.values())

    first = (left_set - right_set).pop()
    last = (right_set - left_set).pop()

    queue = organize_queue(left_dict, right_dict, first, last, n)

    print(' '.join(map(str, queue)))

if __name__ == "__main__":
    main()

 	   			   	 	 	   	 	    	 	 	

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define int long long
//dsu?我只会分类讨论,感觉有点像链式前向星?
signed main() {
	int test = 1;
	//cin >> test;
	while (test--) {
		int n;
		cin >> n;
		if (n % 2 == 0) {
			int a[n + 5];
			map<int, int>mp1, mp2;
			for (int i = 0; i < n; i++) {
				int x, y;
				cin >> x >> y;
				mp1[x] = y; //后
				mp2[y] = x; //前
			}
			a[2] = mp1[0];
			a[n - 1] = mp2[0];
			int t = mp1[0];
			for (int i = 4; i <= n; i += 2) {
				a[i] = mp1[t];
				t = mp1[t];
			}
			t = mp2[0];
			for (int i = n - 3; i >= 1; i -= 2) {
				a[i] = mp2[t];
				t = mp2[t];
			}
			for (int i = 1; i <= n; i++) {
				cout << a[i] << " ";
			}
			cout << endl;
		} else {
			map<int, int>mp;
			map<int, int>mp1; //标记后面的就行
			int a[n + 5];
			for (int i = 0; i < n; i++) {
				int x, y;
				cin >> x >> y;
				mp[x]--;
				mp[y]++;
				mp1[x] = y;
			}
			int jishukaitou;
			for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) {
				if (it->second == -1) {
					jishukaitou = it->first;
				}
			}
			a[1] = jishukaitou;
			int t = jishukaitou;
			for (int i = 3; i <= n; i += 2) {
				a[i] = mp1[t];
				t = mp1[t];
			}
			a[2] = mp1[0];
			t = mp1[0];
			for (int i = 4; i <= n; i += 2) {
				a[i] = mp1[t];
				t = mp1[t];
			}
			for (int i = 1; i <= n; i++)
				cout << a[i] << " ";
			cout << endl;
		}
	}
}
/*
1 2 3 4 5
0 2
1 3
2 4
3 5
4 0
*/


Comments

Submit
0 Comments
More Questions

150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle